package com.city.itis.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.city.itis.dao.MemberDao;
import com.city.itis.domain.Member;
import com.city.itis.domain.MemberCategory;


/**
 * 会员Dao实现类
 * @author WY
 *
 */
@Component("memberDao")
public class MemberDaoImpl implements MemberDao 
{
	
	private HibernateTemplate hibernateTemplate;
	public HibernateTemplate getHibernateTemplate() 
	{
		return hibernateTemplate;
	}
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) 
	{
		this.hibernateTemplate = hibernateTemplate;
	}


	@Override
	public int save(Member member) 
	{
		MemberCategory memberCategory=(MemberCategory)this.hibernateTemplate.get(MemberCategory.class,member.getCategory().getId());
		if(memberCategory!=null)
		{
			try
			{
				member.setCategory(memberCategory);
				int id=(Integer)this.hibernateTemplate.save(member);
				return id;
				
			}catch(Exception e)
			{
				return 0;
			}
			
		}
		return 0;
	}

	@Override
	public int delete(Member member) 
	{
		try 
		{
			this.hibernateTemplate.delete(member);
			
		} catch (Exception e)
		{
			e.printStackTrace();
			//return 0;
		}
			return 1;
	}

	@Override
	public int deleteById(Integer id)
{
		Member member=(Member) this.getHibernateTemplate().get(Member.class, id);
		if(member!=null){
		try {
			this.hibernateTemplate.delete(member);
		} catch (Exception e) 
		{
			e.printStackTrace();
			return 0;
		}
		return 1;
		}
		return 0;
	}

	@Override
	public int update(Member member) 
	{
		MemberCategory memberCategory=(MemberCategory)this.hibernateTemplate.get(MemberCategory.class,member.getCategory().getId());
		if(memberCategory!=null)
		{
			member.setCategory(memberCategory);
			try 
			{
				this.hibernateTemplate.merge(member);
				return 1;
			} catch (Exception e) 
			{
				e.printStackTrace();
				return 0;
			}
		}
		return 0;
	}

	@Override
	public Member getMemberById(Integer id) 
	{
		Member member=(Member)this.getHibernateTemplate().get(Member.class, id);
	     if(member!=null){
	    	 return member;
	     }
		return null;
	}
	
	@Override
	public Member getMemberByName(String name) 
	{
		return null;
	}
	@SuppressWarnings("unchecked")
	@Override
	public Member getMemberByMemberId(String memberId) 
	{
		String queryString="FROM Member m where m.memberId=?";
		List<Member> memberList=(List<Member>)this.getHibernateTemplate().find(queryString,memberId);
		if(memberList!=null)
		{
			for(Member m:memberList)
			{
				return m;
			}
		}
		return null;
	}



	@Override
	public List<Member> findAll() 
	{
		String queryString="FROM Member order by id desc ";
		@SuppressWarnings("unchecked")
		List<Member> memberList=(List<Member>)this.hibernateTemplate.find(queryString);
		if(memberList!=null){
			return memberList;
		}
		return null;
	}
	
	@Override
	public int login(Integer id,String password)
	{
		Member member=(Member)this.getHibernateTemplate().get(Member.class, id);
	     if(member!=null){
	    	 if(member.getPassword().equals(password))
	    	 {
	    		 return 1;
	    	 }
	     }
		return 0;
	}
	@SuppressWarnings("unchecked")
	@Override
	public Member login(String memberId, String password) 
	{
		String queryString="FROM Member where memberId=? and password=?";
		Object values[]={memberId,password};
		List<Member> memberList=(List<Member>)this.hibernateTemplate.find(queryString,values);
		if(memberList!=null)
		{
			for(Member member:memberList)
			{
				return member;
			}
		}
		return null;
	}
	
}
